//---------------------------------------------------------------------------

#include <vcl.h>
#include <Registry.hpp> //Pour TRegistry
#pragma hdrstop

#include "Eic.h"
#include "MTAG_Outils.h"
#include "uPrmProxy.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

#define MODULE_TAG MTAG_UPRMPROXY

/*******************************************************************************

Fonction :

Paramètres d'entrée :

Paramètres modifiés :

Valeur retournée :
*******************************************************************************/
void __fastcall ParamProxy(TComponent* Owner)
{
TscrPrmProxy *scrPrmProxy = new TscrPrmProxy(Owner);
try
   {
   scrPrmProxy->ShowModal();
   }
catch(Exception &e)
   {
   EICDisplayException(TAG(10), e);
   }
delete scrPrmProxy;
}

/*******************************************************************************

Fonction :

Paramètres d'entrée :

Paramètres modifiés :

Valeur retournée :
*******************************************************************************/
PACKAGE_OUTILS void __fastcall GetParamProxy(AnsiString *aAdrHTTP,
                                             short *shPortHTTP,
                                             bool *bAvecIdentification,
                                             AnsiString *aLogin,
                                             AnsiString *aMotDePasse
                                             )
{
TRegistry *pReg = new TRegistry;
try
   {
   if (aAdrHTTP)
      *aAdrHTTP = EmptyStr;
   if (shPortHTTP)
      *shPortHTTP = 0;
   if (bAvecIdentification)
      *bAvecIdentification = false;
   if (aLogin)
      *aLogin = EmptyStr;
   if (aMotDePasse)
      *aMotDePasse = EmptyStr;

   if (pReg->OpenKey("Software\\EIC\\Internet\\Proxy", false))
      {
      if (aAdrHTTP && pReg->ValueExists("AdrProxyHTTP"))
         *aAdrHTTP = pReg->ReadString("AdrProxyHTTP");
      if (shPortHTTP && pReg->ValueExists("PortProxyHTTP"))
         *shPortHTTP = pReg->ReadInteger("PortProxyHTTP");
      if (bAvecIdentification && pReg->ValueExists("Identif"))
         *bAvecIdentification = pReg->ReadBool("Identif");
      if (aLogin && pReg->ValueExists("Login"))
         *aLogin = pReg->ReadString("Login");
      if (aMotDePasse && pReg->ValueExists("MdP"))
         *aMotDePasse = DecryptPassword(pReg->ReadString("MdP").c_str());
      }
   }
catch(Exception &e)
   {
   EICDisplayException(TAG(15), e);
   }
delete pReg;    pReg = NULL;
}

/*******************************************************************************

Fonction :

Paramètres d'entrée :

Paramètres modifiés :

Valeur retournée :
*******************************************************************************/
__fastcall TscrPrmProxy::TscrPrmProxy(TComponent* Owner)
   : TForm(Owner)
{
AnsiString aAdrHTTP;
short shPortHTTP;
bool bAvecIdentification;
AnsiString aLogin;
AnsiString aMdP;
try
   {
   GetParamProxy(&aAdrHTTP, &shPortHTTP, &bAvecIdentification, &aLogin, &aMdP);

   ceAdrProxyHTTP  ->Text    = aAdrHTTP;
   cePortProxyHTTP ->Text    = shPortHTTP;
   ckIdentification->Checked = bAvecIdentification;
   ceLogin         ->Text    = aLogin;
   ceMotDePasse    ->Text    = aMdP;

   }
catch(Exception &e)
   {
   EICDisplayException(TAG(20), e);
   }
}
//---------------------------------------------------------------------------
void __fastcall TscrPrmProxy::btnOkClick(TObject *Sender)
{
TRegistry *pReg = new TRegistry;
char *szMdPCrypte = NULL;
try
   {
   if (pReg->OpenKey("Software\\EIC\\Internet\\Proxy", true))
      {
      pReg->WriteString("AdrProxyHTTP", ceAdrProxyHTTP->Text.Trim());
      pReg->WriteInteger("PortProxyHTTP", cePortProxyHTTP->Text.Trim().IsEmpty() ? 0 : cePortProxyHTTP->Text.ToInt());

      pReg->WriteBool("Identif", !ceAdrProxyHTTP->Text.Trim().IsEmpty() && ckIdentification->Checked);
      pReg->WriteString("Login", ceLogin->Text);

      szMdPCrypte = new char[ceMotDePasse->Text.Length()+1];
      EncryptPassword(ceMotDePasse->Text, szMdPCrypte);
      pReg->WriteString("MdP", szMdPCrypte);
      delete []szMdPCrypte;       szMdPCrypte = NULL;
      }
   }
catch(Exception &e)
   {
   EICDisplayException(TAG(30), e);
   }
delete pReg;    pReg = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TscrPrmProxy::cePortProxyHTTPKeyPress(TObject *Sender,
      char &Key)
{
if ((Key < '0' || Key > '9') && Key != '\b')
   Key = '\0';   
}
//---------------------------------------------------------------------------
void __fastcall TscrPrmProxy::ckIdentificationClick(TObject *Sender)
{
try
   {
   labLogin     ->Enabled = ckIdentification->Checked;
   ceLogin      ->Enabled = ckIdentification->Checked;
   ceLogin      ->Color   = ckIdentification->Checked ? clWindow : clBtnFace;
   labMotDePasse->Enabled = ckIdentification->Checked;
   ceMotDePasse ->Enabled = ckIdentification->Checked;
   ceMotDePasse ->Color   = ckIdentification->Checked ? clWindow : clBtnFace;
   }
catch(Exception &e)
   {
   EICDisplayException(TAG(50), e);
   }
}
//---------------------------------------------------------------------------

